// offLineScore.js
//设置有网络时检测是否有未上传的离线评分
function setupOffLineScoreNotifi(){
	// findOffLineScore();//第一次进入应用检测一次
	api.addEventListener({
	    name:'online'
	}, function(ret, err){  
	    if(ret.connectionType == 'wifi'){//wifi时检测是否有离线提交的数据
    		findOffLineScore();
	    }
	});
	api.addEventListener({
	    name: 'downloadScoreNotifi'
	}, function(ret, err) {
	    var chapterData = ret.value.chapterData;
	    cacheChapterData(chapterData);
	});
	api.addEventListener({
	    name: 'downloadTeacherScoreNotifi'
	}, function(ret, err) {
	    var chapterData = ret.value.chapterData;
	    cacheTeacherChapterData(chapterData);
	});
}
//检测检测是否有未上传的离线评分
function findOffLineScore(){
	var fs = api.require('fs');
	fs.readDir({
	    path: api.cacheDir + '/offLineScoreFolder'
	}, function(ret, err) {
	    if (ret.status) {
	        ergodicFile(ret.data);
	    } else {
	        console.log(JSON.stringify(err));
	    }
	});
}
//遍历文件
function ergodicFile(fileNameArr){
	for (var i = 0; i < fileNameArr.length; i++) {
		var fileName = fileNameArr[i];
		var filePath = api.cacheDir + '/offLineScoreFolder/' + fileName;
		readFileData(filePath);
	}
}
function readFileData(filePath){
	readData(filePath, function(ret, cacheData) {
		if (ret == true) {
			var cacheObjc = JSON.parse(cacheData);
			uploadOffLineData(filePath, cacheObjc);
		}
	});
}
//提交数据
function uploadOffLineData(filePath, cacheObjc){
	uploadData(cacheObjc, function(){
		deleteCacheScoreData(filePath);
	});
}
function uploadData(cacheObjc, callback){
	cfnetppPOST(cacheObjc.url, cacheObjc.param.values, false, function(data, status){
		if (status == 'success') {
			if (data.code == 200 || data.code == 114) {
				$api.setStorage('tmpSelectedUid', '');
				$api.setStorage('tmpSelectedName', '');
				api.sendEvent({
					name : 'freeCourseRecordNotifi',
					extra : {
						uid : $api.getStorage('cfnetppuid'),
						jid : $api.getStorage('cfnetppjxid'),
						cid : cacheObjc.param.cid,
						vid : cacheObjc.param.vid, //章节id
						pid : cacheObjc.param.pid
					}
				});
				callback();
			}
		}
	});
}
//缓存离线数据
function cacheScoreData(type, param, url){//type分team小组评分、self自我评分、teacher教师评分
	var fileName = MD5(param.uid + param.jid + param.pid + param.cid + param.vid + type);
	var filePath = api.cacheDir + '/offLineScoreFolder/' + fileName + '.txt';
	var saveParams = {
		param : param,
		url : url
	};
	var dataStr = JSON.stringify(saveParams);
	writeData(filePath, dataStr, function(ret, err) {
		if (ret) {
			updateLocalData(param, type);
		}
	});
}
function updateLocalData(changeData, type){
	if (type == 'self') {
		updateSelfScore(changeData);
	}else if(type == 'team'){//小组无需更新

	}
}
function updateSelfScore(changeData){
	var selfScoreArr = JSON.parse(changeData.values.data);
	var params = {
		vid : parseInt(changeData.vid),
		uid : changeData.uid,
		jid : changeData.jid,
		pid : changeData.pid,
		cid : changeData.cid
	};
	var fileName = MD5(url_getSitenowScorelist + JSON.stringify(params));
	var fileP = api.cacheDir + '/requestCache/' + fileName + '.txt';
	readData(fileP, function(ret, cacheData) {
		if (ret == true) {
			var cacheObjc = JSON.parse(cacheData);
			var scorelist = cacheObjc.data.scorelist;
			for (var i = 0; i < selfScoreArr.length; i++) {
				scorelist[i].rescore = selfScoreArr[i].score;
			}
			writeData(fileP, JSON.stringify(cacheObjc), function(ret, err) {});
		}
	});
	var resultFileName = MD5(url_getSitenowRealdScore + JSON.stringify(params));
	var resultFilePath = api.cacheDir + '/requestCache/' + resultFileName + '.txt';
	readData(resultFilePath, function(ret, cacheData) {
		if (ret == true) {
			var cacheObjc = JSON.parse(cacheData);
			var scoreArr = cacheObjc.data;
			for (var i = 0; i < selfScoreArr.length; i++) {
				scoreArr[i].myselfscore = selfScoreArr[i].score;
			}
			writeData(resultFilePath, JSON.stringify(cacheObjc), function(ret, err) {});
		}
	});
}
//删除缓存离线数据
function deleteCacheScoreData(filePath){
	var fs = api.require('fs');
	fs.remove({
	    path: filePath
	}, function(ret, err) {
	    if (ret.status) {
	        console.log(JSON.stringify(ret));
	    } else {
	        console.log(JSON.stringify(err));
	    }
	});
}
//缓存授课现场实操数据
function cacheTeacherChapterData(chapterData){
	cacheNetScoreData(chapterData, function(chapterData1){
		cacheTeachScoreResultData(chapterData1, function(chapterData2){
			cacheTeacherStuListData(chapterData2, function(chapterData3){
				//发送缓存完成通知
				api.sendEvent({
				    name: 'cacheNetTeacherScoreDataNotifi',
				    extra: {
				        chaData: chapterData3
				    }
				});
			});
		})
	});
}
//缓存授课评分结果页
function cacheTeachScoreResultData(chapterData, callback){
	var params = {
        uid : $api.getStorage('cfnetppuid'),
        jid : $api.getStorage('cfnetppjxid'),
        pid: chapterData.pid,
        vid: chapterData.vid
    };
	cfnetppPOST(url_getTeacherScore, params, true, function(data, status) {
    	callback(chapterData);    
    });
}
//缓存教师选择小组人员列表数据
function cacheTeacherStuListData(chapterData, callback){
	var params = {
    	uid : $api.getStorage('cfnetppuid'),
    	jid : $api.getStorage('cfnetppjxid'),
    	pid: chapterData.pid,
        vid: chapterData.vid
    };
    cfnetppPOST(url_getStudyList,params,true,function(data, status){
    	callback(chapterData);
    });
}
//缓存章节数据
function cacheChapterData(chapterData){
	cacheNetScoreData(chapterData, function(chapterData1){
		cacheScoreResultData(chapterData1, function(chapterData2){
			cacheStuListData(chapterData2, function(chapterData3){
				//发送缓存完成通知
				api.sendEvent({
				    name: 'cacheNetScoreDataNotifi',
				    extra: {
				        chaData: chapterData3
				    }
				});
			});
		});
	});
}
//缓存现场实操评分数据
function cacheNetScoreData(chapterData, callback){
	var params = {
		vid : chapterData.vid,
		uid : $api.getStorage('cfnetppuid'),
		jid : $api.getStorage('cfnetppjxid'),
		pid : chapterData.pid,
		cid : chapterData.cid
	};
	cfnetppPOST(url_getSitenowScorelist, params, true, function(data, status){
		callback(chapterData);
	});
}
//缓存评分结果业数据
function cacheScoreResultData(chapterData, callback){
	var params = {
        vid : chapterData.vid,
        uid : $api.getStorage('cfnetppuid'),
        jid : $api.getStorage('cfnetppjxid'),
        pid : chapterData.pid,
        cid : chapterData.cid,
	};
	cfnetppPOST(url_getSitenowRealdScore, params, true, function(data, status){
		callback(chapterData);
	});
}
//缓存小组人员列表数据
function cacheStuListData(chapterData, callback){
	var params = {
    	uid : $api.getStorage('cfnetppuid'),
    	jid : $api.getStorage('cfnetppjxid'),
    	pid : chapterData.pid,
    	vid : chapterData.vid,
    	cid : chapterData.cid
    };
    cfnetppPOST(url_getSitenowScoreUser,params,true,function(data, status){
    	callback(chapterData);
    });
}